from util.image_util import recognize_captcha
from selenium import webdriver
from time import sleep
from PIL import Image

url="http://wechat-rect.wechat-saas.192.168.8.90.nip.io:31078/#/"
code_img_path="images/code.png"
test_file_path='‪D:/cache/cat.jpg'

driver=webdriver.Chrome()
driver.get(url)
driver.implicitly_wait(10)

driver.find_element_by_id("inputEmail").send_keys("13075819118")
driver.find_element_by_id("inputPassword").send_keys("123456")
code=driver.find_element_by_id('code')
driver.save_screenshot(code_img_path)

x=code.location['x']+164
y=code.location['y']+120
width=code.size['width']+10
height=code.size['height']
image=Image.open(code_img_path).crop((x,y,x+width,y+height)).save(code_img_path)

code=recognize_captcha(code_img_path)
print("获取的code："+code)
driver.find_element_by_id("check_pin").send_keys(code)
#点击登录
driver.find_element_by_id("submit").click()
#一秒后点击登录
sleep(1)
driver.find_elements_by_class_name("ant-btn").pop().click()

driver.maximize_window()
driver.implicitly_wait(10)

js='document.getElementsByClassName("index-wxid-list")[0].style.backgroundColor="#00FF00"'
driver.execute_script(js)


#点击我的账号
driver.find_element_by_xpath('//*[@id="app"]/div/div[2]/div[2]/div[1]/div[1]/div[2]/ul[2]/li/div[1]/span').click()
#点击会话
driver.find_element_by_css_selector('#app > div > div.container > div:nth-child(2) > div:nth-child(1) > div.index-wxid-list.cover > div.indexwxlist-con > ul:nth-child(2) > li')
#点击通信录
driver.find_element_by_xpath('//*[@id="app"]/div/div[2]/div[2]/div[1]/div[2]/div[1]/span[2]/i').click()
# #点击群聊
# driver.find_element_by_xpath('//*[@id="indexsection-group-h3"]/div[1]/div[1]').click()
# #点击测试群
# driver.find_element_by_xpath('//*[@id="100008900"]').click()
# #发送群聊文字测试
# driver.find_element_by_xpath('//*[@id="contact-send-textarea"]').send_keys("自动化文字测试")
# #点击发送
# driver.find_element_by_xpath('//*[@id="sendBtn"]').click()
#点击朋友圈
driver.find_element_by_xpath('//*[@id="circle"]/a').click()
driver.implicitly_wait(10)
#点击添加微信号
driver.find_element_by_xpath('//*[@id="app"]/div/div[2]/div[4]/div[2]/div/div[2]/img').click()
#点击118
driver.find_element_by_xpath('/html/body/div[2]/div/div/div/div[2]/div/div/div/div[2]/div[2]/div[2]/div').click()
driver.find_element_by_xpath('/html/body/div[2]/div/div/div/div[2]/div/div/div/div[2]/div[2]/div[2]/div[2]/label').click()
#点击保存
driver.find_element_by_css_selector('body > div:nth-child(12) > div > div > div > div.ant-popover-inner > div > div > div > div:nth-child(2) > div.circle-send-wx-select-footer > button:nth-child(2)').click()
#输入文字
driver.find_element_by_css_selector('#app > div > div.container > div:nth-child(4) > div.right-con.cover > div > div.circle-send-input-container > textarea').send_keys('自动化发送朋友圈文字测试')
#发送图片文件
#driver.find_element_by_css_selector('#app > div > div.container > div:nth-child(4) > div.right-con.cover > div > div:nth-child(6) > div:nth-child(3) > div > span > div.ant-upload.ant-upload-select.ant-upload-select-picture > span > input[type=file]').send_keys(test_file_path)
#driver.implicitly_wait(10)
#driver.find_element_by_css_selector('#app > div > div.container > div:nth-child(4) > div.right-con.cover > div > div:nth-child(8) > div.circle-send-comment-container > textarea').send_keys('自动发送评论')
#滚动到屏幕底部
js='window.scrollTo(1000,1000)'
driver.execute_script(js)
#点击发送
driver.find_element_by_css_selector('#app > div > div.container > div:nth-child(4) > div.right-con.cover > div > div.circle-send-button-container > a').click()
sleep(2)
#点击知道了
driver.find_element_by_css_selector('body > div:nth-child(14) > div > div.ant-modal-wrap > div > div.ant-modal-content > div > div > div.ant-confirm-btns > button > span').click()

driver.quit(20)
